package com.exercise.hospital.controller;

import com.exercise.hospital.beans.Hospital;
import com.exercise.hospital.service.HospitalService;
import com.exercise.hospital.utils.TimeUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import java.text.ParseException;
import java.util.Date;
import java.util.List;

/**
 * HospitalController
 *
 * @author admin
 */

@Controller
@RequestMapping("/hospital")
public class HospitalController {

    @Autowired
    private HospitalService hospitalService;

    /**
     * 展示所有 hospital 信息
     *
     * @param model
     * @return
     */
    @RequestMapping("/getAllHospital")
    public String getAllHospital(Model model) {
        List<Hospital> hospitals = hospitalService.getAllHospital();
        model.addAttribute("hospitals", hospitals);
        return "info";
    }

    /**
     * 条件查询 Hospital
     *
     * @return
     */
    @RequestMapping("/getHospitalByCondition")
    public String getHospitalByCondition(String name, String type, String department, Model model) {
        List<Hospital> hospitals = hospitalService.getHospitalByCondition(name, type, department);
        model.addAttribute("hospitals", hospitals);
        return "info";
    }

    /**
     * 前往挂号页面
     */
    @RequestMapping("/toRegistrationInfo")
    public String toRegistrationInfo() {
        return "addInfo";
    }

    /**
     * 进行挂号
     */
    @RequestMapping("/registration")
    public String registration(Hospital hospital, String time){
        try {
            Date date = TimeUtil.charToDate(time);
            hospital.setRegisterTime(date);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        hospitalService.registration(hospital);
        return "redirect:/hospital/getAllHospital";
    }

    /**
     * 核销挂号信息
     */
    @RequestMapping("/updateRegisterInfo/{id}")
    public String updateRegisterInfo(@PathVariable("id") String id){
        hospitalService.updateRegisterInfo(Integer.parseInt(id));
        return "redirect:/hospital/getAllHospital";
    }
}
